<template>
	<view class="h-fill content">
		<u-navbar title="注册达人" titleColor="#fff" backIconColor="#fff" :background="navBg" :borderBottom="false"></u-navbar>
		<u-cell-group :border="false">
			<u-cell-item label="昵称" :arrow="false" :borderBottom="false" hover-class="none">
				<template v-slot:right-icon="">
					<text v-if="userInfo.nickname">{{userInfo.nickname}}</text>
					<get-wx-userinfo v-else @change='handleBind'>
						<text style="color: #19be6b;">使用微信昵称</text>
					</get-wx-userinfo>
				</template>
			</u-cell-item>
			<u-cell-item v-if="userInfo.avatar" label="头像" :arrow="false" :borderBottom="false" hover-class="none">
				<template v-slot:right-icon="">
					<image :src="userInfo.avatar" class="user-head"></image>
				</template>
			</u-cell-item>
			<u-cell-item label="手机号" :arrow="false" :borderBottom="false" hover-class="none">
				<template v-slot:right-icon="">
					<text v-if="userInfo.mobile">{{userInfo.mobile}}</text>
					<get-phone-button v-else :customStyle="{color: '#19be6b'}"></get-phone-button>
				</template>
			</u-cell-item>
		</u-cell-group>
		<view class="pad-base">
			<nice-red-button @click="handleRegister" text="注册"></nice-red-button>
		</view>
		
		<popup-wx-public v-model="wxPublicVisible" :params='params'></popup-wx-public>
	</view>
</template>

<script>
	import {
		registerShareExpert,
		getUserInfo,
		getAccount,
		updateUserInfo
	} from "../../api/platform/user.js"
	import {
		mapState,
		mapMutations
	} from "vuex"
	const reg = /register-share-expert\?codeId=\w{1,}@/
	const reg1 = /&codeCity=\d{4}/
	const reg2 = /&nameCity=(.+)/
	export default {
		data() {
			return {
				params: null,
				isWxPubUser: false,
				wxPublicVisible: false,
				navBg: {
					'background-image': 'linear-gradient(to right, #434AD8, #8E77F5)'
				},
				code: {}
			}
		},
		computed: {
			...mapState(["userInfo", 'wxLoginCode'])
		},
		onLoad(option) {
			console.log(option);
			if (option.q) {
				let url = decodeURIComponent(option.q);
				if (!reg.test(url)) {
					uni.switchTab({
						url: "../../pages/user/user"
					})
					return;
				}
				let codeId, codeCity, nameCity;
				let matches = reg.exec(url);
				if (matches.length) {
					codeId = matches[0].replace("register-share-expert\?codeId=", '').replace('@','');
					codeCity = reg1.exec(url)[0].substr(10, 4)
					nameCity = reg2.exec(url)[0].substr(10)
				}
				if (!codeId) {
					uni.switchTab({
						url: "../../pages/user/user"
					})
					return;
				}
				this.code._id = codeId;
				this.code.codeCity = codeCity;
				this.code.nameCity = nameCity;
			} else if (option.scene) {
				this.code._id = option.scene;
			} else if (option.codeId) {
				this.code._id = option.codeId;
				this.code.codeCity = option.codeCity;
				this.code.nameCity = option.nameCity;
			}
			this.loadData();
		},
		methods: {
			...mapMutations(["updateUserInfo", 'setWxLoginCode']),
			async handleBind(data) {
				try {
					data.code = this.wxLoginCode
					let userInfo = await updateUserInfo(data)
					this.updateUserInfo(userInfo);
					this.$toast("设置成功")
				} catch (e) {
					this.$log("更新用户信息失败", e);
					if (e.code == 500) {
						uni.login({
							success: (codeRes) => {
								data.code = codeRes.code
								this.setWxLoginCode(codeRes.code)
								this.handleBind(data)
							},
							fail: (err) => {
								this.$showError(err)
							}
						})
					}
				}
			},
			async handleRegister() {
				if (this.busy) {
					return;
				}
				this.busy = true;
				if (!this.userInfo.mobile) {
					uni.showToast({
						title: "请先绑定手机号",
						icon: "none"
					})
					this.busy = false;
					return;
				}
				if (!this.userInfo.nickname) {
					uni.showToast({
						title: "请设置昵称",
						icon: "none"
					})
					this.busy = false;
					return;
				}
				try{
					await registerShareExpert({
						codeId: this.code._id,
						codeCity: this.code.codeCity,
					});
					this.busy = false;
					console.log("注册达人成功");
					this.$nextTick(()=>{
						uni.showModal({
							title: "成功",
							content: "恭喜，您已是达人了",
							cancelText: "回主页",
							confirmText: "达人后台",
							success: (res) => {
								if (res.confirm) {
									uni.redirectTo({
										url: "./share-expert-home"
									})
								} else {
									uni.switchTab({
										url: "../../pages/platform/goods-list"
									})
								}
							}
						})
					})
				}catch(e){
					console.log(e);
					if (e.code === 501) {
						this.wxPublicVisible = true
					}
					this.busy = false;
					this.$log("注册达人遇到错误", e);
				}
			},
			async loadData() {
				try{
					let currentRegion = uni.getStorageSync('currentRegion')
					let currentCityCode = currentRegion.city.value
					let userInfo = await getUserInfo({}, currentCityCode)
					this.updateUserInfo(userInfo)
					// this.params = await getAccount({
					// 	showLoading: true,
					// 	codeId: this.code._id
					// })
					this.params = this.code
				}catch(e){
					this.$log("加载会员信息遇到错误");
				}
			}
		}
	}
</script>

<style>

</style>
